package com.ruoyi.web.controller.exa;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.exa.domain.*;
import com.ruoyi.exa.service.*;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.api.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试批次Controller
 * 
 * @author linziheng
 * @date 2025-06-13
 */
@RestController
@RequestMapping("/exa/bitch")
public class ExaBitchController extends BaseController
{
    @Autowired
    private IExaBitchService exaBitchService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private IExaBitchUserService  exaBitchUserService;
    @Autowired
    private ISysPostService iSysPostService;
    @Autowired
    private IExaParperService iExaParperService;
    @Autowired
    private IExaBitchUserService iExaBitchUserService;
    @Autowired
    private IExaParperClassService iExaParperClassService;
    @Autowired
    private IExaParperService exaParperService;
    @Autowired
    private IExaParperClassService exaParperClassService;
    @Autowired
    private IExaBitchUserQuestionsService exaBitchUserQuestionsService;
    @Autowired
    private IExaQuestionBankService exaQuestionBankService;
    @Autowired
    private IExaClassQuestionsService exaClassQuestionsService;
    @Autowired
    private IExaQuestionOptionsService exaQuestionOptionsService;

    /**
     * 查询考试批次列表
     */
    @PreAuthorize("@ss.hasPermi('exa:bitch:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExaBitch exaBitch)
    {
        startPage();
        List<ExaBitch> exaBitchList = exaBitchService.selectExaBitchList(exaBitch);
        //岗位
        List<SysPost> sysPosts = iSysPostService.selectPostAll();
        //试卷
        List<ExaParper> exaParperList = iExaParperService.selectExaParperList(new ExaParper());
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("space");
        //工区
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(sysDictData);
        for (ExaBitch e : exaBitchList) {
            List<SysPost> collectPost = sysPosts.stream().filter(s -> s.getPostId().toString().equals(e.getSysPost())).collect(Collectors.toList());
            List<SysDictData> collectSpace = sysDictDataList.stream().filter(s -> s.getDictValue().equals(e.getSpace())).collect(Collectors.toList());
            List<ExaParper> collectParPer = exaParperList.stream().filter(s -> Objects.equals(s.getId(), e.getParperId())).collect(Collectors.toList());
            e.setSysPostName(collectPost.get(0).getPostName());
            e.setSpaceName(collectSpace.get(0).getDictLabel());
            e.setParperName(collectParPer.get(0).getParperName());
            //当前时间
            Date nowDate = DateUtils.getNowDate();
            int compareToStart = nowDate.compareTo(e.getStartTime());
            int compareToEnd = nowDate.compareTo(e.getEndTime());
            String status = e.getStatus();
            if(compareToStart<0){
                //比开始时间小 0未开始
                e.setStatus("0");
            }else if(compareToEnd>0){
                //比结束时间大 1已结束
                e.setStatus("1");
            }else if(compareToStart>=0&&compareToEnd<=0&&!status.equals("1")){
                //处于开始结束之间 2进行中
                e.setStatus("2");
            }
            exaBitchService.updateExaBitch(e);

        }
        return getDataTable(exaBitchList);
    }

    @ApiOperation("试卷统计")
    @GetMapping("/bitchStatistics")
    public TableDataInfo bitchStatistics(ExaBitch exaBitch){
        startPage();
        List<ExaBitch> exaBitchList = exaBitchService.selectExaBitchList(exaBitch);
        List<ExaBitchParPerResultVo> voList = new ArrayList<>();
        for (ExaBitch bitch:exaBitchList){
            ExaBitchParPerResultVo exaBitchParPerResultVo = new ExaBitchParPerResultVo();
            //批次名
            exaBitchParPerResultVo.setBitchName(bitch.getBitchName());
            exaBitchParPerResultVo.setBitchId(bitch.getId());

            ExaParper exaParper = iExaParperService.selectExaParperById(bitch.getParperId());
            //试卷名
            exaBitchParPerResultVo.setParperName(exaParper.getParperName());
            //总分数
            exaBitchParPerResultVo.setScore(exaParper.getParperScore());
            exaBitchParPerResultVo.setParperId(exaParper.getId());
            exaBitchParPerResultVo.setParperNum(exaParper.getQuestionNum());


            ExaBitchUser exaBitchUser = new ExaBitchUser();
            exaBitchUser.setBitchId(bitch.getId());
            List<ExaBitchUser> exaBitchUserList = iExaBitchUserService.selectExaBitchUserList(exaBitchUser);
            //总人数
            exaBitchParPerResultVo.setPeopleNum(exaBitchUserList.size());
            ExaParperClass exaParperClass = new  ExaParperClass();
            exaParperClass.setParperId(exaParper.getId());
            List<ExaParperClass> exaParperClassList = iExaParperClassService.selectExaParperClassList(exaParperClass);

            //单选
            List<ExaParperClass> collectSingle = exaParperClassList.stream().filter(p -> p.getType().equals("1")).collect(Collectors.toList());
            if(!collectSingle.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectSingle.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setSingleChoice(questions.size());
            }

            //多选
            List<ExaParperClass> collectMultiple = exaParperClassList.stream().filter(p -> p.getType().equals("2")).collect(Collectors.toList());
            if(!collectMultiple.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectMultiple.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setMultipleChoice(questions.size());
            }
            //判断
            List<ExaParperClass> collectJudge = exaParperClassList.stream().filter(p -> p.getType().equals("3")).collect(Collectors.toList());
            if(!collectJudge.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectJudge.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setJudge(questions.size());
            }
            ///简答
            List<ExaParperClass> collectShortAnswer = exaParperClassList.stream().filter(p -> p.getType().equals("4")).collect(Collectors.toList());
            if(!collectShortAnswer.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectShortAnswer.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setShortAnswer(questions.size());
            }

            //通过率
            if(exaBitchUserList.size()>0){
                List<ExaBitchUser> collectPass = exaBitchUserList.stream().filter(e -> e.getIsPass().equals("1")).collect(Collectors.toList());
                exaBitchParPerResultVo.setPassRate(new BigDecimal(collectPass.size()).divide(new BigDecimal(exaBitchUserList.size()),2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            }else {
                exaBitchParPerResultVo.setPassRate(new BigDecimal(0));
            }
            voList.add(exaBitchParPerResultVo);
        }


        return getDataTable(voList);
    }

    /**
     *  题目统计
     * @param parperId 试卷Id
     * @param bitchId 批次Id
     * @return
     */
    @ApiOperation("题目统计")
    @GetMapping("/topicStatistics")
    public R topicStatistics(Long parperId, Long bitchId) {

        //获取试卷信息
        ExaParper exaParper = exaParperService.selectExaParperById(parperId);
        ExaParperClassVo exaParperClassVo = new ExaParperClassVo();


        //获取所有的class
        ExaParperClass exaParperClassParam = new ExaParperClass();
        exaParperClassParam.setParperId(parperId);
        List<ExaParperClass> exaParperClassList = exaParperClassService.selectExaParperClassList(exaParperClassParam);

        List<ExaClassVo> ExaClassVos = new ArrayList<>();

        exaParperClassList.forEach(item->{
            //根据classId获取class详情
            ExaClassVo exaClassVo = new ExaClassVo();

            ExaParperClass exaParperClass = exaParperClassService.selectExaParperClassById(item.getId());
            //根据classId获取class下的所有question
            ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
            exaClassQuestions.setClassId(item.getId());
            List<ExaClassQuestions> exaClassQuestionsList = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
            exaClassVo.setExaParperClass(exaParperClass);
            List<ExaClassQuestionsVo> exaClassQuestionsVos = new ArrayList<>();

            exaClassQuestionsList.forEach(item1-> {
                ExaClassQuestionsVo exaClassQuestionsVo = new ExaClassQuestionsVo();
                exaClassQuestionsVo.setExaClassQuestions(item1);
                //根据questionId获取question详情
                ExaQuestionBankVo exaQuestionBankVo = new ExaQuestionBankVo();
                ExaQuestionBank exaQuestionBank = exaQuestionBankService.selectExaQuestionBankById(item1.getQuestionId());
                //根据questionId获取question下的所有options
                ExaQuestionOptions exaQuestionOptions = new ExaQuestionOptions();
                exaQuestionOptions.setQuestionId(item1.getQuestionId());
                List<ExaQuestionOptions> exaQuestionOptionsList = exaQuestionOptionsService.selectExaQuestionOptionsList(exaQuestionOptions);
                //获取之前提交过的答题卡
                ExaBitchUserQuestions exaBitchUserQuestions = new ExaBitchUserQuestions();
                if(bitchId!=null){
                    exaBitchUserQuestions.setParperId(parperId);
                    exaBitchUserQuestions.setBitchId(bitchId);
                }else {
                    exaBitchUserQuestions.setBitchId(parperId);
                }
                List<ExaBitchUserQuestions> exaBitchUserQuestionsList = exaBitchUserQuestionsService.selectExaBitchUserQuestionsList(exaBitchUserQuestions);
                List<ExaBitchUserQuestions> collectBankQuestions = exaBitchUserQuestionsList.stream().filter(b -> b.getQuestionId() == exaQuestionBank.getId()).collect(Collectors.toList());
                if(exaBitchUserQuestionsList.size()>0){
                    List<ExaBitchUserQuestions> collect = exaBitchUserQuestionsList.stream().filter(q -> q.getQuestionId().equals(item1.getQuestionId())).collect(Collectors.toList());
                    //饼状图
                    JSONArray pieChart = new JSONArray();
                    //柱状图
                    JSONObject histogram = new JSONObject();
                    //拼装柱状图
                    JSONArray name = new JSONArray();
                    JSONArray value = new JSONArray();
                    JSONObject xAxis = new JSONObject();
                    JSONObject yAxis = new JSONObject();
                    for (ExaQuestionOptions questionOptions:exaQuestionOptionsList){
                        Integer choiceNum = 0;
                        for(ExaBitchUserQuestions questions: collect){
                            if(questionOptions.getId().equals(questions.getOptionsId())){
                                //找到考生答的选项有相同的就加1个
                                choiceNum = choiceNum+1;
                            }
                        }
                        //人数小计
                        questionOptions.setChoiceNum(choiceNum);
                        //人数比例
                        if(collectBankQuestions.size()>0){
                            BigDecimal divide = new BigDecimal(choiceNum).divide(new BigDecimal(collectBankQuestions.size()), 2, RoundingMode.HALF_UP);
                            questionOptions.setChoiceRatio(divide.multiply(new BigDecimal(100)));
                        }else {
                            questionOptions.setChoiceRatio(new BigDecimal(0));
                        }

                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name",questionOptions.getContent());
                        jsonObject.put("value",choiceNum);
                        pieChart.add(jsonObject);

                        //拼装柱状图
                        name.add(questionOptions.getContent());
                        value.add(choiceNum);


                    }
                    xAxis.put("data",name);
                    yAxis.put("data",value);
                    histogram.put("xAxis",xAxis);
                    histogram.put("yAxis",yAxis);

                    exaQuestionBank.setHistogram(histogram);
                    exaQuestionBank.setData(pieChart);
                }
                exaQuestionBank.setValidNum(collectBankQuestions.size());
                exaQuestionBankVo.setExaQuestionBank(exaQuestionBank);
                exaQuestionBankVo.setExaQuestionOptionsList(exaQuestionOptionsList);
                exaClassQuestionsVo.setExaQuestionBankVo(exaQuestionBankVo);

                exaClassQuestionsVos.add(exaClassQuestionsVo);
            });
            exaClassVo.setExaClassQuestionsList(exaClassQuestionsVos);
            ExaClassVos.add(exaClassVo);
        });
        exaParperClassVo.setExaClassVoList(ExaClassVos);
        exaParperClassVo.setExaParper(exaParper);
        return R.ok(exaParperClassVo);

    }

    /**
     * 导出试卷统计
     */
    @PreAuthorize("@ss.hasPermi('exa:bitch:export')")
    @Log(title = "试卷统计", businessType = BusinessType.EXPORT)
    @PostMapping("/exportStatistics")
    public void exportStatistics(HttpServletResponse response, ExaBitch exaBitch){
        List<ExaBitch> exaBitchList = exaBitchService.selectExaBitchList(exaBitch);
        List<ExaBitchParPerResultVo> voList = new ArrayList<>();
        for (ExaBitch bitch:exaBitchList){
            ExaBitchParPerResultVo exaBitchParPerResultVo = new ExaBitchParPerResultVo();
            //批次名
            exaBitchParPerResultVo.setBitchName(bitch.getBitchName());
            exaBitchParPerResultVo.setBitchId(bitch.getId());

            ExaParper exaParper = iExaParperService.selectExaParperById(bitch.getParperId());
            //试卷名
            exaBitchParPerResultVo.setParperName(exaParper.getParperName());
            //总分数
            exaBitchParPerResultVo.setScore(exaParper.getParperScore());
            exaBitchParPerResultVo.setParperId(exaParper.getId());
            exaBitchParPerResultVo.setParperNum(exaParper.getQuestionNum());


            ExaBitchUser exaBitchUser = new ExaBitchUser();
            exaBitchUser.setBitchId(bitch.getId());
            List<ExaBitchUser> exaBitchUserList = iExaBitchUserService.selectExaBitchUserList(exaBitchUser);
            //总人数
            exaBitchParPerResultVo.setPeopleNum(exaBitchUserList.size());
            ExaParperClass exaParperClass = new  ExaParperClass();
            exaParperClass.setParperId(exaParper.getId());
            List<ExaParperClass> exaParperClassList = iExaParperClassService.selectExaParperClassList(exaParperClass);

            //单选
            List<ExaParperClass> collectSingle = exaParperClassList.stream().filter(p -> p.getType().equals("1")).collect(Collectors.toList());
            if(!collectSingle.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectSingle.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setSingleChoice(questions.size());
            }

            //多选
            List<ExaParperClass> collectMultiple = exaParperClassList.stream().filter(p -> p.getType().equals("2")).collect(Collectors.toList());
            if(!collectMultiple.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectMultiple.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setMultipleChoice(questions.size());
            }
            //判断
            List<ExaParperClass> collectJudge = exaParperClassList.stream().filter(p -> p.getType().equals("3")).collect(Collectors.toList());
            if(!collectJudge.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectJudge.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setJudge(questions.size());
            }
            ///简答
            List<ExaParperClass> collectShortAnswer = exaParperClassList.stream().filter(p -> p.getType().equals("4")).collect(Collectors.toList());
            if(!collectShortAnswer.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectShortAnswer.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setShortAnswer(questions.size());
            }

            //通过率
            if(exaBitchUserList.size()>0){
                List<ExaBitchUser> collectPass = exaBitchUserList.stream().filter(e -> e.getIsPass().equals("1")).collect(Collectors.toList());
                exaBitchParPerResultVo.setPassRate(new BigDecimal(collectPass.size()).divide(new BigDecimal(exaBitchUserList.size()),2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            }else {
                exaBitchParPerResultVo.setPassRate(new BigDecimal(0));
            }
            voList.add(exaBitchParPerResultVo);
        }

        ExcelUtil<ExaBitchParPerResultVo> util = new ExcelUtil<ExaBitchParPerResultVo>(ExaBitchParPerResultVo.class);
        util.exportExcel(response, voList, "试卷统计");
    }

    /**
     * 导出考试批次列表
     */
    @PreAuthorize("@ss.hasPermi('exa:bitch:export')")
    @Log(title = "考试批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExaBitch exaBitch)
    {
        List<ExaBitch> list = exaBitchService.selectExaBitchList(exaBitch);
        //岗位
        List<SysPost> sysPosts = iSysPostService.selectPostAll();
        //试卷
        List<ExaParper> exaParperList = iExaParperService.selectExaParperList(new ExaParper());
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("space");
        //工区
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(sysDictData);
        for (ExaBitch e : list) {
            List<SysPost> collectPost = sysPosts.stream().filter(s -> s.getPostId().toString().equals(e.getSysPost())).collect(Collectors.toList());
            List<SysDictData> collectSpace = sysDictDataList.stream().filter(s -> s.getDictValue().equals(e.getSpace())).collect(Collectors.toList());
            List<ExaParper> collectParPer = exaParperList.stream().filter(s -> Objects.equals(s.getId(), e.getParperId())).collect(Collectors.toList());
            e.setSysPostName(collectPost.get(0).getPostName());
            e.setSpaceName(collectSpace.get(0).getDictLabel());
            e.setParperName(collectParPer.get(0).getParperName());

        }
        ExcelUtil<ExaBitch> util = new ExcelUtil<ExaBitch>(ExaBitch.class);
        util.exportExcel(response, list, "考试批次数据");
    }

    /**
     * 获取考试批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('exa:bitch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(exaBitchService.selectExaBitchById(id));
    }

    /**
     * 新增考试批次
     */
    @PreAuthorize("@ss.hasPermi('exa:bitch:add')")
    @Log(title = "考试批次", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody ExaBitch exaBitch)
    {
        int result = 0;
        if(1==exaBitchService.insertExaBitch(exaBitch)){
            List<SysUser> sysUsers = sysUserService.selectUserByPostAndSpace(exaBitch.getSysPost(), exaBitch.getSpace());
            //获取工区信息
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictType("space");
            sysDictData.setDictValue(exaBitch.getSpace());
            List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(sysDictData);
            for (int i = 0; i < sysUsers.size(); i++) {
                //组装批次员工
                ExaBitchUser exaBitchUser = new ExaBitchUser();
                exaBitchUser.setBitchId(exaBitch.getId());
                exaBitchUser.setBitchName(exaBitch.getBitchName());
                exaBitchUser.setUserId(sysUsers.get(i).getUserId());
                exaBitchUser.setUserName(sysUsers.get(i).getNickName());
                exaBitchUser.setParperId(exaBitch.getParperId());
                exaBitchUser.setSpaceName(sysDictDataList.get(0).getDictLabel());
                exaBitchUser.setSpaceId(sysDictDataList.get(0).getDictValue());
                exaBitchUser.setSysPostId(exaBitch.getSysPost());
                SysPost sysPost = iSysPostService.selectPostById(Long.parseLong(exaBitch.getSysPost()));
                exaBitchUser.setSysPostName(sysPost.getPostName());
                exaBitchUserService.insertExaBitchUser(exaBitchUser);
            }
            result = 1;
        }else {
            result=0;
        }
        return toAjax(result);
    }

    /**
     * 修改考试批次
     */
    @PreAuthorize("@ss.hasPermi('exa:bitch:edit')")
    @Log(title = "考试批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExaBitch exaBitch)
    {
        Long[] bitchIds = new Long[1];
        bitchIds[0] = exaBitch.getId();
        //先删除批次员工表  再添加
        exaBitchUserService.deleteExaBitchUserByBitchId(bitchIds);
        List<SysUser> sysUsers = sysUserService.selectUserByPostAndSpace(exaBitch.getSysPost(), exaBitch.getSpace());
        //获取工区信息
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("space");
        sysDictData.setDictValue(exaBitch.getSpace());
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(sysDictData);
        for (int i = 0; i < sysUsers.size(); i++) {
            //组装批次员工
            ExaBitchUser exaBitchUser = new ExaBitchUser();
            exaBitchUser.setBitchId(exaBitch.getId());
            exaBitchUser.setBitchName(exaBitch.getBitchName());
            exaBitchUser.setUserId(sysUsers.get(i).getUserId());
            exaBitchUser.setUserName(sysUsers.get(i).getUserName());
            exaBitchUser.setParperId(exaBitch.getParperId());
            exaBitchUser.setSpaceName(sysDictDataList.get(0).getDictLabel());
            exaBitchUserService.insertExaBitchUser(exaBitchUser);
        }

        return toAjax(exaBitchService.updateExaBitch(exaBitch));
    }

    @ApiOperation("回收试卷")
    @GetMapping("/recovery")
    public AjaxResult recovery(Long id){
        ExaBitch exaBitch = exaBitchService.selectExaBitchById(id);
        ExaBitchUser exaBitchUser = new ExaBitchUser();
        exaBitchUser.setBitchId(id);
        List<ExaBitchUser> exaBitchUsers = exaBitchUserService.selectExaBitchUserList(exaBitchUser);
        for (ExaBitchUser bitchUser:exaBitchUsers){
            if(!bitchUser.getIsPass().equals("1")){
                //判断是否有交卷操作  空就没交过卷
                if(bitchUser.getCommitTime()==null){
                    bitchUser.setStatus("4");
                }else {
                    bitchUser.setStatus("3");
                }
                bitchUser.setIsPass("0");
                exaBitchUserService.updateExaBitchUser(bitchUser);
            }
        }
        exaBitch.setStatus("1");
        return toAjax(exaBitchService.updateExaBitch(exaBitch));
    }

    /**
     * 删除考试批次
     */
    @PreAuthorize("@ss.hasPermi('exa:bitch:remove')")
    @Log(title = "考试批次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //先删除批次员工表
        exaBitchUserService.deleteExaBitchUserByBitchId(ids);
        return toAjax(exaBitchService.deleteExaBitchByIds(ids));
    }
}
